<?php 
namespace app\index\model;
use think\Model;
use think\Db;
class Customer extends Model
{
    public function check_customer($uid=null){
        if($uid != null){
            $map = array(
                "id" => $uid,
                "account" => $uid,
                "phonenum" => $uid,
                "cardnum" => $uid,
            );
        }else{
            $map = "";
        }
        if($map == ""){
            return array("status"=>0,"data"=>[]);
        }
        $data=Db::name('laytp_user')
            ->field('id')
            ->whereOr($map)
            ->find();
        if($data){
            return array("status"=>1,"data"=>$data);
        }else{
            return array("status"=>0,"data"=>[]);
        }
    }

    public function search_customer($uid=null, $page=1, $limit=5){
        if($uid != null){
            $map = array(
                'laytp_user.id' => $uid,
                'laytp_user.account' => $uid,
                'laytp_user.phonenum' => $uid,
                'laytp_user.cardnum' => $uid,
            );
        }else{
            $map = "1=1";//where true
        }
        $data = Db::name('laytp_user')
            ->alias('a')
            ->field('
                a.id as uid, 
                a.account as account, 
                a.nickname as nickname,
                a.phonenum as phonenum,
                a.telnum as telnum,
                a.idcardnum as idcardnum, 
                a.birthday as birthday,
                a.sex as sex,
                a.cardnum as cardnum,
                a.scores_simple as scores_simple,
                a.scores_consumption as scores_consumption,
                b.amount as amount,
                b.status as status
            ')
            ->join('laytp_card b', 'a.cardnum = b.cardnum', 'LEFT')
            ->whereOr($map)
            ->page($page)
            ->limit($limit)
            //->fetchSql(true)
            ->select();
        //dump($data);
        $count = Db::name('laytp_user')
            -> where($map)
            -> count();
        if($data){
            return array("status"=>1, "count"=>$count, "data"=>$data);
        }else{
            return array("status"=>0, "count"=>0, "data"=>[]);
        }
    }

    public function account_flow($uid=null, $page=1, $limit=5){
        //用户信息，里面包含了积分汇总信息
        $user = $this -> search_customer($uid, $page, $limit);
        $recharge = $this -> recharge_total($user['data'][0]['cardnum']);
        $consumption = $this -> consumption_total($user['data'][0]['cardnum']);
        $cash = $this -> cash_total($user['data'][0]['cardnum']);
        return array(
            "status"=>1,
            "data"=>array(
                "recharge" => $recharge['data']['amount'],//充值金额汇总
                "consumption"=>$consumption['data']['amount'],//消费金额汇总
                "cash"=>$cash['data']['amount'],//提现金额汇总
                "scores_consumption"=>$user['data'][0]['scores_consumption'],//消费积分汇总
                "scores_simple"=>$user['data'][0]['scores_simple'],//常规积分汇总
            )
        );
    }

    //充值记录明细
    public function recharge($uid=null, $page=1, $limit=5){
        $user = $this -> search_customer($uid, $page, $limit);
        if($uid != null){
            $map = array(
                'cardnum' => $user['data'][0]['cardnum'],
            );
        }else{
            $map = "1=1";//where true
        }
        $data = Db::table('laytp_recharge')
            ->alias('a')
            ->field('
                a.cardnum as cardnum,
                a.amount as amount,
                a.type as type,
                a.time as time,
                a.attach as attach,
                a.worker as worker
            ')
            ->where($map)
            ->page($page)
            ->limit($limit)
            //->fetchsql(true)
            ->select();
        $count = Db::name('laytp_recharge')
            ->where($map)
            -> count();
        if($data){
            return array("status"=>1, "count"=>$count, "data"=>$data);
        }else{
            return array("status"=>0, "count"=>0, "data"=>[]);
        }
    }
    public function recharge_group_list($cardnum=null, $platform=null, $page=1, $limit=5){
        if($cardnum == null && $platform == null){
            $map = "1=1";//where true
        }else{
            if($platform != null){
                switch ($platform)
                {
                    case 'x':
                        $platform = '现金';
                        break;
                    case 'n':
                        $platform = '网络';
                        break;       
                    default:
                        $platform = '';
                        break;
                }
                $map['type'] = $platform;
            }
            if($cardnum != null){
                $map['cardnum'] = $cardnum;
            }
        }
        $data = Db::table('laytp_recharge')
            ->alias('a')
            ->field('
                a.cardnum as cardnum,
                a.amount as amount,
                a.type as type,
                a.time as time,
                a.attach as attach,
                a.worker as worker
            ')
            ->where($map)
            ->page($page)
            ->limit($limit)
            //->fetchsql(true)
            ->select();
        $count = Db::name('laytp_recharge')
            ->where($map)
            ->count();
        if($data){
            return array("status"=>1, "count"=>$count, "data"=>$data);
        }else{
            return array("status"=>0, "count"=>0, "data"=>[]);
        }
    }
    public function recharge_group_total($platform=null)
    {
        if($platform != null){
            switch ($platform)
            {
                case 'x':
                    $platform = '现金';
                    break;
                case 'n':
                    $platform = '网络';
                    break;       
                default:
                    $platform = '';
                    break;
            }
            $map = array(
                'type' => $platform,
            );
        }else{
            $map = "1=1";//where true
        }
        $data = Db::table('laytp_recharge')
            ->alias('a')
            ->field('
                SUM(a.amount) as amount
            ')
            ->where($map)
            ->find();
        if($data){
            return array("status"=>1, "data"=>$data);
        }else{
            return array("status"=>0, "data"=>[]);
        }
    }

    //消费记录明细
    public function consumption($uid=null, $page=1, $limit=5){
        $user = $this -> search_customer($uid, $page, $limit);
        if($uid != null){
            $map = array(
                'cardnum' => $user['data'][0]['cardnum'],
            );
        }else{
            $map = "1=1";//where true
        }
        $data = Db::table('laytp_consumption')
            ->alias('a')
            ->field('
                a.cardnum as cardnum,
                a.amount as amount,
                a.type as type,
                a.time as time,
                a.attach as attach,
                a.worker as worker
            ')
            ->where($map)
            ->page($page)
            ->limit($limit)
            //->fetchsql(true)
            ->select();
        $count = Db::name('laytp_consumption')
            ->where($map)
            -> count();
        if($data){
            return array("status"=>1, "count"=>$count, "data"=>$data);
        }else{
            return array("status"=>0, "count"=>0, "data"=>[]);
        }
    }

    //提现记录明细
    public function cash($uid=null, $page=1, $limit=5){
        $user = $this -> search_customer($uid, $page, $limit);
        if($uid != null){
            $map = array(
                'cardnum' => $user['data'][0]['cardnum'],
            );
        }else{
            $map = "1=1";//where true
        }
        $data = Db::table('laytp_cash')
            ->alias('a')
            ->field('
                a.cardnum as cardnum,
                a.amount as amount,
                a.type as type,
                a.time as time,
                a.name as name,
                a.account as account,
                a.attach as attach,
                a.worker as worker,
                a.status as status
            ')
            ->where($map)
            ->page($page)
            ->limit($limit)
            //->fetchsql(true)
            ->select();
        $count = Db::name('laytp_cash')
            ->where($map)
            -> count();
        if($data){
            return array("status"=>1, "count"=>$count, "data"=>$data);
        }else{
            return array("status"=>0, "count"=>0, "data"=>[]);
        }
    }
    
    //消费积分变动明细
    public function scores_consumption($uid=null, $page=1, $limit=5){
        $user = $this -> search_customer($uid, $page, $limit);
        if($uid != null){
            $map = array(
                'userid' => $user['data'][0]['uid'],
            );
        }else{
            $map = "1=1";//where true
        }
        $data = Db::table('laytp_scores_consumption')
            ->alias('a')
            ->field('
                a.id as id,
                a.userid as userid,
                a.scores as scores,
                a.type as type,
                a.time as time,
                a.attach as attach,
                a.worker as worker
            ')
            ->where($map)
            ->page($page)
            ->limit($limit)
            //->fetchsql(true)
            ->select();
        $count = Db::name('laytp_scores_consumption')
            ->where($map)
            -> count();
        if($data){
            return array("status"=>1, "count"=>$count, "data"=>$data);
        }else{
            return array("status"=>0, "count"=>0, "data"=>[]);
        }
    }

    //等级（常规）积分变动明细
    public function scores_simple($uid=null, $page=1, $limit=5){
        $user = $this -> search_customer($uid, $page, $limit);
        if($uid != null){
            $map = array(
                'userid' => $user['data'][0]['uid'],
            );
        }else{
            $map = "1=1";//where true
        }
        $data = Db::table('laytp_scores_simple')
            ->alias('a')
            ->field('
                a.id as id,
                a.userid as userid,
                a.scores as scores,
                a.type as type,
                a.time as time,
                a.attach as attach,
                a.worker as worker
            ')
            ->where($map)
            ->page($page)
            ->limit($limit)
            //->fetchsql(true)
            ->select();
        $count = Db::name('laytp_scores_simple')
            ->where($map)
            -> count();
        if($data){
            return array("status"=>1, "count"=>$count, "data"=>$data);
        }else{
            return array("status"=>0, "count"=>0, "data"=>[]);
        }
    }

    public function cash_list($page=1, $limit=5)
    {
        $data = Db::name('laytp_cash')
            ->field('
                id as id, 
                cardnum as cardnum, 
                amount as amount, 
                time as time, 
                type as type, 
                name as name, 
                account as account, 
                attach as attach, 
                worker as worker, 
                status as status
            ')
            ->page($page)
            ->limit($limit)
            ->select();
        $count = Db::name('laytp_cash')
            -> count();
        if($data){
            return array("status"=>1, "count"=>$count, "data"=>$data);
        }else{
            return array("status"=>0, "count"=>0, "data"=>[]);
        }
    }

    public function cash_total($cardnum=null){
        if($cardnum != null){
            $map['cardnum'] = $cardnum;
        }else{
            $map = "1=1";//where true
        }
        $data = Db::table('laytp_cash')
            ->alias('a')
            ->field('SUM(a.amount) as amount')
            ->where($map)
            //->fetchsql(true)
            ->find();
        if($data){
            return array("status"=>1, "data"=>$data);
        }else{
            return array("status"=>0);
        }
    }
    
    public function consumption_total($cardnum=null){
        if($cardnum != null){
            $map['cardnum'] = $cardnum;
        }else{
            $map = "1=1";//where true
        }
        $data = Db::table('laytp_consumption')
            ->alias('a')
            ->field('SUM(a.amount) as amount')
            ->where($map)
            //->fetchsql(true)
            ->find();
        if($data){
            return array("status"=>1, "data"=>$data);
        }else{
            return array("status"=>0);
        }
    }

    public function recharge_total($cardnum=null){
        if($cardnum != null){
            $map['cardnum'] = $cardnum;
        }else{
            $map = "1=1";//where true
        }
        $data = Db::table('laytp_recharge')
            ->alias('a')
            ->field('SUM(a.amount) as amount')
            ->where($map)
            //->fetchsql(true)
            ->find();
        if($data){
            return array("status"=>1, "data"=>$data);
        }else{
            return array("status"=>0);
        }
    }

    public function customer_level(){
        $data = Db::table('laytp_user')
            ->field('level, COUNT(id) as total')
            ->group('level')
            //->fetchsql(true)
            ->select();
        if($data){
            return $data;
        }else{
            return "";
        }
    }

    public function customer_lose(){
        $time = time();
        $data = Db::table('laytp_user')
            ->field('
                nickname as nickname,
                phonenum as phonenum,
                last_time as last_time
            ')
            ->where('('.$time.'-last_time)/(60*60*24) > 90')
            //->fetchsql(true)
            ->select();
        if($data){
            return $data;
        }else{
            return "";
        }
    }
    
    public function customer_total($cardnum=null){
        $data = Db::table('laytp_user')
            ->field('COUNT(id) as total')
            ->select();
        if($data){
            return $data[0]['total'];
        }else{
            return 0;
        }
    }
    public function customer_amount_total($cardnum=null){
        $data = Db::table('laytp_card')
            ->field('SUM(amount) as total')
            ->select();
        if($data){
            return $data[0]['total'];
        }else{
            return 0;
        }
    }
    public function customer_scores_consumption_total($cardnum=null){
        $data = Db::table('laytp_user')
            ->field('SUM(scores_consumption) as total')
            ->select();
        if($data){
            return $data[0]['total'];
        }else{
            return 0;
        }
    }
}